package com.example.test_witns.ui.add_friends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.test_witns.R;
import com.example.test_witns.databinding.ActivityAddFriendBinding;
import com.example.test_witns.ui.add_search_result.AddSearchResultActivity;
import com.example.test_witns.ui.my_qrcode.MyQRCodeActivity;
import com.example.test_witns.ui.my_qrcode.VerticalScannerActivity;
import com.example.test_witns.utils.MyInfosHelper;

import java.util.ArrayList;
import java.util.List;


public class AddFriendsActivity extends Activity {
    private ActivityAddFriendBinding binding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAddFriendBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        View root = binding.getRoot();

        MyInfosHelper myInfosHelper = new MyInfosHelper(getApplicationContext());

        if (myInfosHelper.checkIfLogin()){
            List<String> list = new ArrayList<String>();
            list.add(myInfosHelper.getMyNickname());
            list.add(myInfosHelper.getMyUserId());
            list.add(myInfosHelper.getMyUUID());
            list.add(myInfosHelper.getMyAvatarURL());
            setRedirectListener(root.findViewById(R.id.add_friends_my_qr_code), MyQRCodeActivity.class, myInfosHelper.getAvatarByName(myInfosHelper.getMyUserId()), list);
        }else {
            root.findViewById(R.id.my_qr_code).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toast_Prohibited_Style("您尚未登录");
                }
            });
        }

        View add_friends_qr_code_scan = findViewById(R.id.add_friends_qr_code_scan);
        add_friends_qr_code_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), VerticalScannerActivity.class);
                startActivity(intent);
            }
        });

        View backTofriends = root.findViewById(R.id.chat_backToFriends);
        backTofriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        View add_options = root.findViewById(R.id.add_options);

        SearchView search_to_add = root.findViewById(R.id.search_to_add);
        search_to_add.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                String searchArg = query;
                Intent intent = new Intent(getApplicationContext(), AddSearchResultActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("queryString", query);
                startActivity(intent);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (search_to_add.getQuery().toString().isEmpty()){
                    add_options.setVisibility(View.VISIBLE);
                }else {
                    add_options.setVisibility(View.GONE);
                }
                return false;
            }
        });
    }

    public void setRedirectListener(View listenerView, Class c, byte[] argumentBytes, List<String> argumentStrings){
        listenerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddFriendsActivity.this, c);
                intent.putExtra("argumentBytes", argumentBytes);
                int argumentStringCounter = 1;
                for(String argumentString : argumentStrings){
                    intent.putExtra("argumentString" + argumentStringCounter++, argumentString);
                }
                startActivity(intent);
            }
        });
    }

    private void toast_Prohibited_Style(String message) {
        View customView = LayoutInflater.from(AddFriendsActivity.this).inflate(R.layout.custom_toast_prohibited_layout, null);
        TextView toastText = customView.findViewById(R.id.toast_message);
        toastText.setText(message);

        Toast toast = new Toast(AddFriendsActivity.this);
        toast.setView(customView);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 40); // 设置在屏幕顶部
        toast.show();
    }
}
